home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / Caml Light 0.7 / examples / minilogo / alex.ml < prev    next >
Encoding:
Text File  |  1995-06-01  |  1.7 KB  |  57 lines  |  [TEXT/MPS ]

  1. let rec saute_blancs flux =
  2.   match flux with
  3.     [< ' ` ` >] -> saute_blancs flux  (* ` ` est l'espace *)
  4.   | [< ' `\t` >] -> saute_blancs flux (* `\t` est la tabulation *)
  5.   | [< ' `\n` >] -> saute_blancs flux (* `\n` est la fin de ligne *)
  6.   | [< >] -> ();;
  7.  
  8. let rec saute_blancs flux =
  9.   match flux with
  10.     [< ' (` ` | `\t` | `\n`) >] -> saute_blancs flux
  11.   | [< >] -> ();;
  12.  
  13. let rec lire_entier accumulateur flux =
  14.   match flux with
  15.     [< '(`0`..`9` as c) >] ->
  16.       lire_entier (10 * accumulateur + int_of_char c - 48) flux
  17.   | [< >] -> accumulateur;;
  18.  
  19. let rec lire_décimales accumulateur échelle flux =
  20.   match flux with
  21.     [< '(`0`..`9` as c) >] ->
  22.       lire_décimales
  23.         (accumulateur +.
  24.            float_of_int(int_of_char c - 48) *. échelle)
  25.         (échelle /. 10.0) flux
  26.   | [< >] -> accumulateur;;
  27.  
  28. let tampon = "----------------";;
  29.  
  30. let rec lire_mot position flux =
  31.   match flux with
  32.     [< '(`A`..`Z` | `a`..`z` | `é` | `è` | `_` as c) >] ->
  33.       if position < string_length tampon then
  34.         set_nth_char tampon position c;
  35.       lire_mot (position+1) flux
  36.   | [< >] ->
  37.       sub_string tampon 0 (min position (string_length tampon));;
  38.  
  39. let rec lire_lexème flux =
  40.   saute_blancs flux;
  41.   match flux with
  42.     [< '(`A`..`Z` | `a`..`z` | `é` | `è` as c) >] ->
  43.       set_nth_char tampon 0 c;
  44.       Mot(lire_mot 1 flux)
  45.   | [< '(`0`..`9` as c) >] ->
  46.       let n = lire_entier (int_of_char c - 48) flux in
  47.       begin match flux with
  48.         [< '`.` >] ->
  49.           Constante_flottante
  50.             (lire_décimales (float_of_int n) 0.1 flux)
  51.       | [< >] -> Constante_entière(n)
  52.       end
  53.   | [< 'c >] -> Symbole c;;
  54.  
  55. let analyseur_lexical flux =
  56.   stream_from (fun () -> lire_lexème flux);;
  57.